﻿using System;
using System.Security.Cryptography;
using System.Text;
using Xoohoo.Extensions;

namespace Xoohoo.Utilities.Cryptography
{
    /// <summary>
    /// SHA256加密算法
    /// </summary>
    public static class SHA256
    {
        public static string Encrypt(this string rawString, string salt)
        {
            if (rawString.IsNullOrEmpty()) throw new ArgumentNullException("rawString");
            if (salt.IsNullOrEmpty()) throw new ArgumentNullException("salt");

            return Convert.ToBase64String(EncryptToByteArray(rawString, salt));
        }
        public static byte[] EncryptToByteArray(this string rawString, string salt)
        {
            if (rawString.IsNullOrEmpty()) throw new ArgumentNullException("rawString");
            if (salt.IsNullOrEmpty()) throw new ArgumentNullException("salt");

            byte[] salted = Encoding.UTF8.GetBytes(string.Concat(rawString, salt));
            System.Security.Cryptography.SHA256 hasher = new SHA256Managed();
            byte[] hashed = hasher.ComputeHash(salted);
            return hashed;
        }
    }
}
